﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlFile = "dict.xml";

            string txtFile = "dict.txt";

            if (!File.Exists(xmlFile))
            {
                Console.WriteLine("XML file {0} doesn't exist!", xmlFile);
                return;
            }

            if (File.Exists(txtFile))
            {
                Console.WriteLine("txt file {0} is existing, remove it!", txtFile);
                File.Delete(txtFile);
                //return;
            }

            StreamWriter sw = new StreamWriter(txtFile);

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);

            XmlNode wordBook = doc.SelectSingleNode("wordbook");
            int count = 1;
            foreach (XmlNode item in wordBook.ChildNodes)
            {
                // Write ID first;
                sw.WriteLine(count);
                for (XmlNode childItem = item.FirstChild; childItem != null; childItem = childItem.NextSibling)
                {
                    if (childItem.Name == "word")
                    {
                        sw.WriteLine(childItem.InnerText);
                    }
                    if (childItem.Name == "trans")
                    {
                        string wholeText = childItem.InnerText;
                        int indexofFirstLine = wholeText.IndexOf('\n');
                        if (indexofFirstLine > 0)
                            sw.WriteLine(wholeText.Substring(0, indexofFirstLine-1));
                        else
                            sw.WriteLine(wholeText);
                    }
                }

                sw.WriteLine();

                ++count;
            }

            
        }
    }
}
